package com.meitianhui.convenitenbuy.module.home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.home.contract.IntegrationContract;
import com.meitianhui.convenitenbuy.module.home.di.DaggerHomeComponent;
import com.meitianhui.convenitenbuy.module.home.di.HomeModule;
import com.meitianhui.convenitenbuy.module.home.presenter.IntegrationPresenter;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.ReceivingAddressActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.util.AddressUtil;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.LoadingDialog;
import com.meitianhui.convenitenbuy.widget.dialog.SettingStoreDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/12/2.
 * 积分页面
 */

public class IntegrationActivity extends BaseToolbarActivity implements IntegrationContract.View {
    private TextView mTvName, mTvPhone, mTvAddress;
    private TextView mTvGoodName, mTvIntegration, mTvPayMoney;
    private ImageView mIvGood;
    private String mJson;//传递过来的商品信息
    private String mActivityId;//传递过来的activityid
    private String mGoodId;
    private String mIntegration;
    private int mPayWay = 1;//支付方式 1为积分 2为免单券
    private List<ConsumerAddress.ConsumerData> consumerDataList;
    private SettingStoreDialog mDialog;
    private LoadingDialog mLoadingDialog;
    @Inject
    IntegrationPresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_integration;
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerHomeComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
        initView();
        initData();

    }

    private void initView() {
        isEnable(false);
        setTbTitle("订单支付");
        mIvGood = getViewById(R.id.iv_good);
        mTvGoodName = getViewById(R.id.tv_good_name);
        mTvPayMoney = getViewById(R.id.tv_pay_money);
        mTvIntegration = getViewById(R.id.tv_integration);
        mTvName = getViewById(R.id.tv_name);
        mTvPhone = getViewById(R.id.tv_phone);
        mTvAddress = getViewById(R.id.tv_address);
        RxUtils.bindEvents(getViewById(R.id.rl_address), this);
        RxUtils.bindEvents(getViewById(R.id.tv_pay), this);
    }

    private void initData() {
        Intent intent = getIntent();
        mJson = intent.getStringExtra("json");
        mActivityId = intent.getStringExtra("activity_id");
        try {
            JSONObject jsonObject = new JSONObject(mJson);
            JSONArray jsonArray = jsonObject.getJSONArray("picUrlArr");
            if (jsonArray.length() > 0) {
                //商品图片
                GlideUtils.setCircleImage(this,jsonArray.get(0).toString(), R.mipmap.icon_default_picture_store, R.mipmap.icon_default_picture_store, mIvGood);
            }
            //商品名称
            String title = jsonObject.getString("goods_title");
            if (!TextUtils.isEmpty(title)) {
                mTvGoodName.setText(title);
            }
            mIntegration = jsonObject.getString("limited_point");
            mTvIntegration.setText(mIntegration);
            mTvPayMoney.setText(mIntegration + "积分");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        consumerDataList = InfoProvider.getUserReceivingAddress(this);
        if (consumerDataList != null) {
            if (consumerDataList.size() > 0) {
                for (int i = 0; i < consumerDataList.size(); i++) {
                    if (TextUtils.equals(consumerDataList.get(i).getIs_major_addr(), "Y")) {
                        mTvName.setText(consumerDataList.get(i).getConsignee());
                        mTvPhone.setText(consumerDataList.get(i).getMobile());
                        String area = AddressUtil.getSSQ(this, consumerDataList.get(i).getArea_id());
                        String address = consumerDataList.get(i).getAddress();
                        mTvAddress.setText(area + address);
                    }
                }
            } else {
                mTvName.setText("");
                mTvPhone.setText("");
                mTvAddress.setText("");
            }
        }
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.rl_address:
                Intent intent = new Intent(this, ReceivingAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_pay:    //支付
                String name = mTvName.getText().toString();
                String phone = mTvPhone.getText().toString();
                String address = mTvAddress.getText().toString();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(address)) {
                    TipUtils.showToast(App.getInstance(), "请添加收货地址");
                    return;
                }
                if (mPayWay == 1) {
                    //积分
                    try {
                        mDialog = new SettingStoreDialog(IntegrationActivity.this, R.style.dialog_with_alpha, new SettingStoreDialog.AlertDialogListener() {
                            @Override
                            public void onConfirmClick() {
                                mDialog.dismiss();
                                JSONObject jsonObject = null;
                                try {
                                    jsonObject = new JSONObject(mJson);
                                    mGoodId = jsonObject.getString("goods_id");
                                    mLoadingDialog = new LoadingDialog(IntegrationActivity.this);
                                    presenter.payIntegration(mGoodId, mActivityId);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onCancelClick() {
                                mDialog.dismiss();
                            }
                        });
                        mDialog.show();
                        mDialog.showCancelBtn(true);
                        mDialog.setDialogTitle("提示");
                        mDialog.setDialogContent("是否确认支付?");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    //免单券
                    Intent i = new Intent(IntegrationActivity.this, FreePaperActivity.class);
                    i.putExtra("json", mJson);
                    i.putExtra("activity_id", mActivityId);
                    startActivity(i);
                }
                break;


        }

    }

    @Override
    public void payIntegrationSuccess(EmptyInfo data) {
        if (TextUtils.equals(data.status, "true")) {
            presenter.userIntegrationPay(mIntegration, mGoodId, mActivityId);
        } else {
            TipUtils.showToast(App.getInstance(), "本月兑换次数已用光", 2);
            mLoadingDialog.stopProgressDialog();
        }
    }

    @Override
    public void userIntegrationPaySuccess(EmptyInfo data) {
        mLoadingDialog.stopProgressDialog();
        TipUtils.showToast(App.getInstance(), "支付成功");
        setResult(RESULT_OK);
        finish();

    }

    @Override
    public void onFail(int code, String msg) {
        TipUtils.showToast(App.getInstance(), msg);
        mLoadingDialog.stopProgressDialog();
    }


    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {

    }

}
